Skip to content

fix: the all_permutations in all_permutations.py#14595

Open
orbisai0security wants to merge 3 commits intoTheAlgorithms:masterfrom
orbisai0security:fix-v-002-permutations-input-length-limit
Open

fix: the all_permutations in all_permutations.py#14595
orbisai0security wants to merge 3 commits intoTheAlgorithms:masterfrom
orbisai0security:fix-v-002-permutations-input-length-limit

Conversation

@orbisai0security
Copy link
Copy Markdown

Summary

Fix high severity security issue in backtracking/all_permutations.py.

Vulnerability

Field Value
ID V-002
Severity HIGH
Scanner multi_agent_ai
Rule V-002
File backtracking/all_permutations.py:81

Description: The all_permutations.py script reads an unbounded sequence of integers from CLI input at line 81 and generates all permutations with no upper bound on sequence length. Permutation generation has O(n!) time and memory complexity. With n=20, this produces approximately 2.4 quintillion permutations, exhausting CPU and RAM and rendering the host system unresponsive or triggering an out-of-memory kill of the process.

Changes

  • backtracking/all_permutations.py

Verification

  • Build passes
  • Scanner re-scan confirms fix
  • LLM code review passed

Automated security fix by OrbisAI Security

Automated security fix generated by Orbis Security AI
@algorithms-keeper algorithms-keeper Bot added enhancement This PR modified some existing files awaiting reviews This PR is ready to be reviewed labels Apr 28, 2026
Comment thread backtracking/all_permutations.py Outdated

print("Enter the elements")
sequence = list(map(int, input().split()))
MAX_SEQUENCE_LENGTH = 8
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider moving MAX_SEQUENCE_LENGTH to the module level instead of defining it inside this input block. That makes the limit easier to find, reuse, and update later.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@orbisai0security can you address code review comments?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

Comment thread backtracking/all_permutations.py Outdated
print("Enter the elements")
sequence = list(map(int, input().split()))
MAX_SEQUENCE_LENGTH = 8
user_input = list(map(int, input().split()))
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still only supports integer input from the user, while the function type hints allow both int and str. Consider documenting that interactive input only accepts integers, or update the parsing logic to support strings too.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Comment thread backtracking/all_permutations.py Outdated
sequence = list(map(int, input().split()))
MAX_SEQUENCE_LENGTH = 8
user_input = list(map(int, input().split()))
if len(user_input) > MAX_SEQUENCE_LENGTH:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The length check happens after converting all input values into a list. For very large input, this still loads everything into memory first. Consider checking the split input length before mapping to integers.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

MAX_SEQUENCE_LENGTH = 8
user_input = list(map(int, input().split()))
if len(user_input) > MAX_SEQUENCE_LENGTH:
raise ValueError(f"Input sequence too long (max {MAX_SEQUENCE_LENGTH} elements).")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a test or doctest for the new length limit so this behavior is verified automatically and does not regress later.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@github-actions github-actions Bot force-pushed the fix-v-002-permutations-input-length-limit branch from 1640999 to 61d15e3 Compare May 2, 2026 07:28
@algorithms-keeper algorithms-keeper Bot added the tests are failing Do not merge until tests pass label May 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files tests are failing Do not merge until tests pass

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants